public class MaxSubArraySum {  
  
    public static int maxSubArray(int[] nums) {  
        if (nums == null || nums.length == 0) {  
            return 0;  
        }  
  
        int maxSoFar = nums[0]; 
        int currMax = nums[0]; 
  
        for (int i = 1; i < nums.length; i++) {  
                       currMax = Math.max(nums[i], currMax + nums[i]);  
  
                      maxSoFar = Math.max(maxSoFar, currMax);  
        }  
  
        return maxSoFar;  
    }  
  
    public static void main(String[] args) {  
        int[] nums1 = {1, -2, 3, 5, -1};  
        int[] nums2 = {1, -2, 3, -8, 5, 1};  
        int[] nums3 = {1, -2, 3, -2, 5, 1};  
  
        System.out.println("子数组之和的最大值1: " + maxSubArray(nums1)); 
        System.out.println("子数组之和的最大值2: " + maxSubArray(nums2)); 
        System.out.println("子数组之和的最大值3: " + maxSubArray(nums3)); 
    }  
}